package com.boot.app.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.boot.api.ApiResult;
import com.boot.app.base.domain.BaseRoleAppRelation;
import com.boot.app.base.service.IBaseRoleAppRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "RoleAppRelationController", description = "角色应用关系")
@RestController
@RequestMapping("base/role/app/relation")
public class BaseRoleAppRelationController {

    private final IBaseRoleAppRelationService service;

    @Autowired
    public BaseRoleAppRelationController(
        IBaseRoleAppRelationService service
    ){
        this.service = service;
    }

    @Operation(summary = "获取角色的应用权限")
    @GetMapping("list")
    public ApiResult<List<BaseRoleAppRelation>> getRoleAppRelationList(BaseRoleAppRelation baseRoleAppRelation){
        LambdaQueryWrapper<BaseRoleAppRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseRoleAppRelation::getRoleId, baseRoleAppRelation.getRoleId());
        List<BaseRoleAppRelation> list = service.list(wrapper);
        return ApiResult.success(list);
    }

    @Operation(summary = "为角色分配应用")
    @PostMapping("allocateAppByRoleId")
    public ApiResult<?> postRoleAppRelation(@RequestParam("roleId") long roleId, @RequestParam(value = "appIds") long[] appIds){
        service.saveBatchByRoleIdAndAppIds(roleId, appIds);
        return ApiResult.success();
    }
}
